<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"wan-lei8.gitee.io","root":"/","scheme":"Pisces","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="更新单条记录  UPDATE course SET name &#x3D; &#39;course1&#39; WHEREid &#x3D; &#39;id1&#39;;">
<meta property="og:type" content="article">
<meta property="og:title" content="Mybatis Batch Update">
<meta property="og:url" content="https://wan-lei8.gitee.io/2019/08/19/Mybatis-Batch-Update/index.html">
<meta property="og:site_name" content="程序员西桐">
<meta property="og:description" content="更新单条记录  UPDATE course SET name &#x3D; &#39;course1&#39; WHEREid &#x3D; &#39;id1&#39;;">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2019-08-19T03:06:59.000Z">
<meta property="article:modified_time" content="2021-11-19T03:10:39.908Z">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="https://wan-lei8.gitee.io/2019/08/19/Mybatis-Batch-Update/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>Mybatis Batch Update | 程序员西桐</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">程序员西桐</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">但行好事，莫问前程</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://wan-lei8.gitee.io/2019/08/19/Mybatis-Batch-Update/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.png">
      <meta itemprop="name" content="">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="程序员西桐">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Mybatis Batch Update
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2019-08-19 11:06:59" itemprop="dateCreated datePublished" datetime="2019-08-19T11:06:59+08:00">2019-08-19</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2021-11-19 11:10:39" itemprop="dateModified" datetime="2021-11-19T11:10:39+08:00">2021-11-19</time>
              </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <ol>
<li><h5 id="更新单条记录"><a href="#更新单条记录" class="headerlink" title="更新单条记录"></a>更新单条记录</h5></li>
</ol>
<p><code>UPDATE course SET name = &#39;course1&#39; WHEREid = &#39;id1&#39;;</code></p>
<span id="more"></span>

<ol start="2">
<li><h5 id="更新多条记录的同一个字段为同一个值"><a href="#更新多条记录的同一个字段为同一个值" class="headerlink" title="更新多条记录的同一个字段为同一个值"></a>更新多条记录的同一个字段为同一个值</h5></li>
</ol>
<p><code>UPDATE course SET name=&#39;course1&#39; WHERE id in(&#39;id1&#39;,&#39;id2&#39;,&#39;id3);</code></p>
<ol start="3">
<li><h5 id="更新多条记录为多个字段为不同的值"><a href="#更新多条记录为多个字段为不同的值" class="headerlink" title="更新多条记录为多个字段为不同的值"></a>更新多条记录为多个字段为不同的值</h5><p>比较普通的写法，是通过循环，依次执行update语句</p>
<p>Mybatis写法如下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">&lt;update id=<span class="string">&quot;updateBatch&quot;</span>  parameterType=<span class="string">&quot;java.util.List&quot;</span>&gt;  </span><br><span class="line">    &lt;foreach collection=<span class="string">&quot;list&quot;</span> item=<span class="string">&quot;item&quot;</span> index=<span class="string">&quot;index&quot;</span> open=<span class="string">&quot;&quot;</span> close=<span class="string">&quot;&quot;</span> separator=<span class="string">&quot;;&quot;</span>&gt;</span><br><span class="line">        update course</span><br><span class="line">        &lt;set&gt;</span><br><span class="line">            name=$&#123;item.name&#125;</span><br><span class="line">        &lt;/set&gt;</span><br><span class="line">        where id = $&#123;item.id&#125;</span><br><span class="line">    &lt;/foreach&gt;      </span><br><span class="line">&lt;/update&gt;</span><br></pre></td></tr></table></figure>

<p>一条记录update一次，性能比较差，容易造成阻塞。</p>
<ol start="4">
<li><h5 id="MySQL没有提供直接的方法来实现批量更新，但可以使用case-when语法来实现这个功能。"><a href="#MySQL没有提供直接的方法来实现批量更新，但可以使用case-when语法来实现这个功能。" class="headerlink" title="MySQL没有提供直接的方法来实现批量更新，但可以使用case when语法来实现这个功能。"></a>MySQL没有提供直接的方法来实现批量更新，但可以使用case when语法来实现这个功能。</h5></li>
</ol>
</li>
</ol>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"> UPDATE course</span><br><span class="line">    SET name = CASE id </span><br><span class="line">        WHEN <span class="number">1</span> THEN <span class="string">&#x27;name1&#x27;</span></span><br><span class="line">        WHEN <span class="number">2</span> THEN <span class="string">&#x27;name2&#x27;</span></span><br><span class="line">        WHEN <span class="number">3</span> THEN <span class="string">&#x27;name3&#x27;</span></span><br><span class="line">    END, </span><br><span class="line">    title = CASE id </span><br><span class="line">        WHEN <span class="number">1</span> THEN <span class="string">&#x27;New Title 1&#x27;</span></span><br><span class="line">        WHEN <span class="number">2</span> THEN <span class="string">&#x27;New Title 2&#x27;</span></span><br><span class="line">        WHEN <span class="number">3</span> THEN <span class="string">&#x27;New Title 3&#x27;</span></span><br><span class="line">    <span class="function">END</span></span><br><span class="line"><span class="function">WHERE id <span class="title">IN</span> <span class="params">(<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>)</span></span></span><br></pre></td></tr></table></figure>

<p>这条sql的意思是，如果id为1，则name的值为name1，title的值为New Title1；依此类推。</p>
<p>在Mybatis中的配置则如下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">&lt;updateid=<span class="string">&quot;updateBatch&quot;</span>parameterType=<span class="string">&quot;list&quot;</span>&gt;          </span><br><span class="line">	&lt;trim prefix=<span class="string">&quot;set&quot;</span> suffixOverrides=<span class="string">&quot;,&quot;</span>&gt;</span><br><span class="line">             &lt;trim prefix=<span class="string">&quot;name=case&quot;</span> suffix=<span class="string">&quot;end,&quot;</span>&gt;</span><br><span class="line">                 &lt;foreach collection=<span class="string">&quot;list&quot;</span> item=<span class="string">&quot;item&quot;</span> index=<span class="string">&quot;index&quot;</span>&gt;</span><br><span class="line">                         &lt;<span class="keyword">if</span> test=<span class="string">&quot;item.name!=null&quot;</span>&gt;</span><br><span class="line">                          when id=#&#123;item.id&#125; then #&#123;item.name&#125;</span><br><span class="line">                         &lt;/<span class="keyword">if</span>&gt;</span><br><span class="line">                 &lt;/foreach&gt;</span><br><span class="line">              &lt;/trim&gt;</span><br><span class="line">              &lt;trim prefix=<span class="string">&quot;title =case&quot;</span> suffix=<span class="string">&quot;end,&quot;</span>&gt;</span><br><span class="line">                 &lt;foreach collection=<span class="string">&quot;list&quot;</span> item=<span class="string">&quot;item&quot;</span> index=<span class="string">&quot;index&quot;</span>&gt;</span><br><span class="line">                         &lt;<span class="keyword">if</span> test=<span class="string">&quot;item.title!=null&quot;</span>&gt;</span><br><span class="line">                          when id=#&#123;item.id&#125; then #&#123;item.title&#125;</span><br><span class="line">                         &lt;/<span class="keyword">if</span>&gt;</span><br><span class="line">                 &lt;/foreach&gt;</span><br><span class="line">              &lt;/trim&gt;</span><br><span class="line">             &lt;/trim&gt;</span><br><span class="line">            where</span><br><span class="line">            &lt;foreach collection=<span class="string">&quot;list&quot;</span> separator=<span class="string">&quot;or&quot;</span> item=<span class="string">&quot;item&quot;</span> index=<span class="string">&quot;index&quot;</span>&gt;</span><br><span class="line">              id=#&#123;item.id&#125;</span><br><span class="line">          &lt;/foreach&gt;</span><br><span class="line">&lt;/update&gt;</span><br></pre></td></tr></table></figure>

<p>trim标签属性说明 </p>
<ul>
<li>prefix,suffix 表示在trim标签包裹的部分的前面或者后面添加内容</li>
<li>如果同时有prefixOverrides,suffixOverrides 表示会用prefix,suffix覆盖Overrides中的内容</li>
<li>如果只有prefixOverrides,suffixOverrides 表示删除开头的或结尾的xxxOverides指定的内容</li>
</ul>
<ol start="5">
<li><h5 id="sql批量更新另外一个示例"><a href="#sql批量更新另外一个示例" class="headerlink" title="sql批量更新另外一个示例"></a>sql批量更新另外一个示例</h5></li>
</ol>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">&lt;updateid=<span class="string">&quot;updateBatch&quot;</span>parameterType=<span class="string">&quot;java.util.List&quot;</span>&gt;   </span><br><span class="line">	set  status=</span><br><span class="line">    &lt;foreach collection=<span class="string">&quot;list&quot;</span> item=<span class="string">&quot;item&quot;</span> index=<span class="string">&quot;index&quot;</span> </span><br><span class="line">        separator=<span class="string">&quot; &quot;</span> open=<span class="string">&quot;case ID&quot;</span> close=<span class="string">&quot;end&quot;</span>&gt;</span><br><span class="line">        when #&#123;item.id&#125; then #&#123;item.status&#125;</span><br><span class="line">    &lt;/foreach&gt;</span><br><span class="line">    where id in</span><br><span class="line">    &lt;foreach collection=<span class="string">&quot;list&quot;</span> index=<span class="string">&quot;index&quot;</span> item=<span class="string">&quot;item&quot;</span> </span><br><span class="line">        separator=<span class="string">&quot;,&quot;</span> open=<span class="string">&quot;(&quot;</span> close=<span class="string">&quot;)&quot;</span>&gt;</span><br><span class="line">        #&#123;item.id,jdbcType=BIGINT&#125;</span><br><span class="line">    &lt;/foreach&gt;</span><br><span class="line"> &lt;/update&gt;</span><br></pre></td></tr></table></figure>

<p>其中when…then…是sql中的”switch” 语法。这里借助mybatis的<foreach>语法来拼凑成了批量更新的sql，上面的意思就是批量更新id在updateBatch参数所传递List中的数据的status字段。还可以使用<trim>实现同样的功能,代码如下:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">&lt;update id=<span class="string">&quot;updateBatch&quot;</span> parameterType=<span class="string">&quot;java.util.List&quot;</span>&gt;       </span><br><span class="line">	update mydata_table</span><br><span class="line">        &lt;trim prefix=<span class="string">&quot;set&quot;</span> suffixOverrides=<span class="string">&quot;,&quot;</span>&gt;</span><br><span class="line">            &lt;trim prefix=<span class="string">&quot;status =case&quot;</span> suffix=<span class="string">&quot;end,&quot;</span>&gt;</span><br><span class="line">                &lt;foreach collection=<span class="string">&quot;list&quot;</span> item=<span class="string">&quot;item&quot;</span> index=<span class="string">&quot;index&quot;</span>&gt;</span><br><span class="line">                     when id=#&#123;item.id&#125; then #&#123;item.status&#125;</span><br><span class="line">                &lt;/foreach&gt;</span><br><span class="line">            &lt;/trim&gt;</span><br><span class="line">        &lt;/trim&gt;</span><br><span class="line">        where id in</span><br><span class="line">        &lt;foreach collection=<span class="string">&quot;list&quot;</span> index=<span class="string">&quot;index&quot;</span> item=<span class="string">&quot;item&quot;</span> separator=<span class="string">&quot;,&quot;</span> open=<span class="string">&quot;(&quot;</span> close=<span class="string">&quot;)&quot;</span>&gt;</span><br><span class="line">            #&#123;item.id,jdbcType=BIGINT&#125;</span><br><span class="line">        &lt;/foreach&gt;</span><br><span class="line">&lt;/update&gt;</span><br></pre></td></tr></table></figure>

<p>其结构如下：</p>
<figure class="highlight plaintext"><figcaption><span>mydata_table</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">set status = </span><br><span class="line">case</span><br><span class="line">    when id = #&#123;item.id&#125; then #&#123;item.status&#125;//此处应该是&lt;foreach&gt;展开值</span><br><span class="line">    ...</span><br><span class="line">end</span><br><span class="line">where id in (...);</span><br></pre></td></tr></table></figure>

<p>如果对要更新的数据进行判断,只有符合条件的数据才能进行更新,这种情况可以这么做:</p>
<figure class="highlight plaintext"><figcaption><span>prefix</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">&lt;trim prefix=&quot;status =case&quot; suffix=&quot;end,&quot;&gt;</span><br><span class="line">	&lt;foreach collection=&quot;list&quot; item=&quot;item&quot; index=&quot;index&quot;&gt;</span><br><span class="line">         &lt;if test=&quot;item.status !=null and item.status != -1&quot;&gt;</span><br><span class="line">             when id=#&#123;item.id&#125; then #&#123;item.status&#125;</span><br><span class="line">         &lt;/if&gt;</span><br><span class="line">     &lt;/foreach&gt;</span><br><span class="line">&lt;/trim&gt;</span><br></pre></td></tr></table></figure>

<p>这样的话只有要更新的list中status != null &amp;&amp; status != -1的数据才能进行status更新.其他的将使用默认值更新,而不会保持原数据不变.如果要保持原数据不变呢?即满足条件的更新,不满足条件的保持原数据不变,简单的来做就是再加一个<if>,因为mybatis中没有if…else…语法,但可以通过多个<if>实现同样的效果,如下:</p>
<figure class="highlight plaintext"><figcaption><span>prefix</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">    &lt;trim prefix=&quot;status =case&quot; suffix=&quot;end,&quot;&gt;</span><br><span class="line">    &lt;foreach collection=&quot;list&quot; item=&quot;item&quot; index=&quot;index&quot;&gt;</span><br><span class="line">         &lt;if test=&quot;item.status !=null and item.status != -1&quot;&gt;</span><br><span class="line">             when id=#&#123;item.id&#125; then #&#123;item.status&#125;</span><br><span class="line">         &lt;/if&gt;</span><br><span class="line">         &lt;if test=&quot;item.status == null or item.status == -1&quot;&gt;</span><br><span class="line">             when id=#&#123;item.id&#125; then mydata_table.status      //这里就是原数据</span><br><span class="line">         &lt;/if&gt;</span><br><span class="line">     &lt;/foreach&gt;</span><br><span class="line">&lt;/trim&gt;</span><br></pre></td></tr></table></figure>

<h4 id="整体批量更新的写法如下"><a href="#整体批量更新的写法如下" class="headerlink" title="整体批量更新的写法如下:"></a>整体批量更新的写法如下:</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">&lt;updateid=<span class="string">&quot;updateBatch&quot;</span>parameterType=<span class="string">&quot;java.util.List&quot;</span>&gt;</span><br><span class="line">    update mydata_table</span><br><span class="line">&lt;trim prefix=<span class="string">&quot;set&quot;</span> suffixOverrides=<span class="string">&quot;,&quot;</span>&gt;</span><br><span class="line">            &lt;trim prefix=<span class="string">&quot;status =case&quot;</span> suffix=<span class="string">&quot;end,&quot;</span>&gt;</span><br><span class="line">                 &lt;foreach collection=<span class="string">&quot;list&quot;</span> item=<span class="string">&quot;item&quot;</span> index=<span class="string">&quot;index&quot;</span>&gt;</span><br><span class="line">                     &lt;<span class="keyword">if</span> test=<span class="string">&quot;item.status !=null and item.status != -1&quot;</span>&gt;</span><br><span class="line">                         when id=#&#123;item.id&#125; then #&#123;item.status&#125;</span><br><span class="line">                     &lt;/<span class="keyword">if</span>&gt;</span><br><span class="line">                     &lt;<span class="keyword">if</span> test=<span class="string">&quot;item.status == null or item.status == -1&quot;</span>&gt;</span><br><span class="line">                         when id=#&#123;item.id&#125; then mydata_table.status<span class="comment">//原数据</span></span><br><span class="line">                     &lt;/<span class="keyword">if</span>&gt;</span><br><span class="line">                 &lt;/foreach&gt;</span><br><span class="line">            &lt;/trim&gt;</span><br><span class="line">        &lt;/trim&gt;</span><br><span class="line">        where id in</span><br><span class="line">        &lt;foreach collection=<span class="string">&quot;list&quot;</span> index=<span class="string">&quot;index&quot;</span> item=<span class="string">&quot;item&quot;</span> separator=<span class="string">&quot;,&quot;</span> open=<span class="string">&quot;(&quot;</span> close=<span class="string">&quot;)&quot;</span>&gt;</span><br><span class="line">            #&#123;item.id,jdbcType=BIGINT&#125;</span><br><span class="line">        &lt;/foreach&gt;</span><br><span class="line">&lt;/update&gt;</span><br></pre></td></tr></table></figure>

<ol start="6">
<li><h5 id="组装多个update语句，这种方式需要设置jdbc连接-allowMultiQueries-true"><a href="#组装多个update语句，这种方式需要设置jdbc连接-allowMultiQueries-true" class="headerlink" title="组装多个update语句，这种方式需要设置jdbc连接 allowMultiQueries=true"></a>组装多个update语句，这种方式需要设置jdbc连接 allowMultiQueries=true</h5></li>
</ol>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">url: jdbc:mysql:<span class="comment">//111.11.181.141:1122/$&#123;config.mysql-database&#125;?useSSL=false&amp;useUnicode=true&amp;characterEncoding=UTF8&amp;allowMultiQueries=true</span></span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">&lt;update id=<span class="string">&quot;updateEquementWaterTest&quot;</span>   parameterType=<span class="string">&quot;java.util.List&quot;</span>&gt;</span><br><span class="line">   &lt;foreach collection=<span class="string">&quot;list&quot;</span> item=<span class="string">&quot;item&quot;</span> index=<span class="string">&quot;index&quot;</span>&gt;</span><br><span class="line">	          	update rent_hl_room l</span><br><span class="line">	SET l.water_meter_id=#&#123;item.equipmentCode&#125;,</span><br><span class="line">	l.water_meter_source_type=#&#123;item.equipmentSource&#125;</span><br><span class="line">	WHERE </span><br><span class="line">	    l.room_id=#&#123;item.roomId&#125;;</span><br><span class="line"> 		 &lt;/foreach&gt;</span><br><span class="line">  &lt;/update&gt;</span><br></pre></td></tr></table></figure>

<p>case when 方式</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">UPDATE </span><br><span class="line">  rent_hl_room l </span><br><span class="line">SET</span><br><span class="line">  electricity_meter_id = </span><br><span class="line">  CASE</span><br><span class="line">    WHEN room_id = <span class="number">1942</span></span><br><span class="line">    THEN <span class="number">180524348</span> </span><br><span class="line">    WHEN room_id = <span class="number">1945</span></span><br><span class="line">    THEN <span class="number">180524480</span> </span><br><span class="line">  END,</span><br><span class="line">  electricity_meter_source_type = </span><br><span class="line">  CASE</span><br><span class="line">    WHEN room_id = <span class="number">1942</span> </span><br><span class="line">    THEN ym </span><br><span class="line">    WHEN room_id = <span class="number">1945</span> </span><br><span class="line">    THEN ym </span><br><span class="line">  END </span><br><span class="line">WHERE room_id = <span class="number">1942</span> </span><br><span class="line">  OR room_id = <span class="number">1945</span></span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"> </span><br><span class="line">   &lt;update id=<span class="string">&quot;updateEquementWater&quot;</span>   parameterType=<span class="string">&quot;java.util.List&quot;</span>&gt;</span><br><span class="line">    update rent_hl_room l</span><br><span class="line">     &lt;trim prefix=<span class="string">&quot;set&quot;</span> suffixOverrides=<span class="string">&quot;,&quot;</span>&gt;</span><br><span class="line">      &lt;trim prefix=<span class="string">&quot;water_meter_id =case&quot;</span> suffix=<span class="string">&quot;end,&quot;</span>&gt;</span><br><span class="line">          &lt;foreach collection=<span class="string">&quot;list&quot;</span> item=<span class="string">&quot;i&quot;</span> index=<span class="string">&quot;index&quot;</span>&gt;</span><br><span class="line">                  &lt;<span class="keyword">if</span> test=<span class="string">&quot;i.equipmentCode!=null&quot;</span>&gt;</span><br><span class="line">                   when room_id=#&#123;i.roomId&#125; then #&#123;i.equipmentCode&#125;</span><br><span class="line">                  &lt;/<span class="keyword">if</span>&gt;</span><br><span class="line">          &lt;/foreach&gt;</span><br><span class="line">       &lt;/trim&gt;</span><br><span class="line">       &lt;trim prefix=<span class="string">&quot; water_meter_source_type =case&quot;</span> suffix=<span class="string">&quot;end,&quot;</span>&gt;</span><br><span class="line">          &lt;foreach collection=<span class="string">&quot;list&quot;</span> item=<span class="string">&quot;i&quot;</span> index=<span class="string">&quot;index&quot;</span>&gt;</span><br><span class="line">                  &lt;<span class="keyword">if</span> test=<span class="string">&quot;i.equipmentSource!=null&quot;</span>&gt;</span><br><span class="line">                   when room_id=#&#123;i.roomId&#125; then #&#123;i.equipmentSource&#125;</span><br><span class="line">                  &lt;/<span class="keyword">if</span>&gt;</span><br><span class="line">          &lt;/foreach&gt;</span><br><span class="line">       &lt;/trim&gt;</span><br><span class="line">      &lt;/trim&gt;</span><br><span class="line">     where</span><br><span class="line">     &lt;foreach collection=<span class="string">&quot;list&quot;</span> separator=<span class="string">&quot;or&quot;</span> item=<span class="string">&quot;i&quot;</span> index=<span class="string">&quot;index&quot;</span> &gt;</span><br><span class="line">       room_id=#&#123;i.roomId&#125;</span><br><span class="line">   &lt;/foreach&gt;</span><br><span class="line">	    </span><br><span class="line">&lt;/update&gt;</span><br></pre></td></tr></table></figure>

<p>经测试 100条数据的时候第一种方式的效率比第二种差不多高1倍</p>

    </div>
<div>
  
    <div>
    
        <div style="text-align:center;color: #ccc;font-size:14px;">-------------本文结束<i class="fa fa-paw"></i>感谢您的阅读-------------</div>
    
</div>

  
</div>

    
    
    
        

<div>
<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="https://wan-lei8.gitee.io/2019/08/19/Mybatis-Batch-Update/" title="Mybatis Batch Update">https://wan-lei8.gitee.io/2019/08/19/Mybatis-Batch-Update/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>


      <footer class="post-footer">

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2019/07/17/WorkSummary-QueryInvitee/" rel="prev" title="WorkSummary_QueryInvitee">
      <i class="fa fa-chevron-left"></i> WorkSummary_QueryInvitee
    </a></div>
      <div class="post-nav-item">
    <a href="/2019/12/15/worksummary-seeOperationLog/" rel="next" title="worksummary_seeOperationLog">
      worksummary_seeOperationLog <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-5"><a class="nav-link" href="#%E6%9B%B4%E6%96%B0%E5%8D%95%E6%9D%A1%E8%AE%B0%E5%BD%95"><span class="nav-number">1.</span> <span class="nav-text">更新单条记录</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#%E6%9B%B4%E6%96%B0%E5%A4%9A%E6%9D%A1%E8%AE%B0%E5%BD%95%E7%9A%84%E5%90%8C%E4%B8%80%E4%B8%AA%E5%AD%97%E6%AE%B5%E4%B8%BA%E5%90%8C%E4%B8%80%E4%B8%AA%E5%80%BC"><span class="nav-number">2.</span> <span class="nav-text">更新多条记录的同一个字段为同一个值</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#%E6%9B%B4%E6%96%B0%E5%A4%9A%E6%9D%A1%E8%AE%B0%E5%BD%95%E4%B8%BA%E5%A4%9A%E4%B8%AA%E5%AD%97%E6%AE%B5%E4%B8%BA%E4%B8%8D%E5%90%8C%E7%9A%84%E5%80%BC"><span class="nav-number">3.</span> <span class="nav-text">更新多条记录为多个字段为不同的值</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#MySQL%E6%B2%A1%E6%9C%89%E6%8F%90%E4%BE%9B%E7%9B%B4%E6%8E%A5%E7%9A%84%E6%96%B9%E6%B3%95%E6%9D%A5%E5%AE%9E%E7%8E%B0%E6%89%B9%E9%87%8F%E6%9B%B4%E6%96%B0%EF%BC%8C%E4%BD%86%E5%8F%AF%E4%BB%A5%E4%BD%BF%E7%94%A8case-when%E8%AF%AD%E6%B3%95%E6%9D%A5%E5%AE%9E%E7%8E%B0%E8%BF%99%E4%B8%AA%E5%8A%9F%E8%83%BD%E3%80%82"><span class="nav-number">4.</span> <span class="nav-text">MySQL没有提供直接的方法来实现批量更新，但可以使用case when语法来实现这个功能。</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#sql%E6%89%B9%E9%87%8F%E6%9B%B4%E6%96%B0%E5%8F%A6%E5%A4%96%E4%B8%80%E4%B8%AA%E7%A4%BA%E4%BE%8B"><span class="nav-number">5.</span> <span class="nav-text">sql批量更新另外一个示例</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%95%B4%E4%BD%93%E6%89%B9%E9%87%8F%E6%9B%B4%E6%96%B0%E7%9A%84%E5%86%99%E6%B3%95%E5%A6%82%E4%B8%8B"><span class="nav-number"></span> <span class="nav-text">整体批量更新的写法如下:</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#%E7%BB%84%E8%A3%85%E5%A4%9A%E4%B8%AAupdate%E8%AF%AD%E5%8F%A5%EF%BC%8C%E8%BF%99%E7%A7%8D%E6%96%B9%E5%BC%8F%E9%9C%80%E8%A6%81%E8%AE%BE%E7%BD%AEjdbc%E8%BF%9E%E6%8E%A5-allowMultiQueries-true"><span class="nav-number">1.</span> <span class="nav-text">组装多个update语句，这种方式需要设置jdbc连接 allowMultiQueries&#x3D;true</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt=""
      src="/images/avatar.png">
  <p class="site-author-name" itemprop="name"></p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives">
          <span class="site-state-item-count">41</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/yourname" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;yourname" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="/malegod958@163.com" title="E-Mail → malegod958@163.com"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>
  <div class="cc-license motion-element" itemprop="license">
    <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" class="cc-opacity" rel="noopener" target="_blank"><img src="/images/cc-by-nc-sa.svg" alt="Creative Commons"></a>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 2018 – 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">万磊</span>
</div>
  <div class="powered-by">

  </div>

        








      </div>
    </footer>
  </div>

  
  
  <script color='' opacity='0.5' zIndex='-1' count='99' src="/lib/canvas-nest/canvas-nest.min.js"></script>
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  















  

  

  
	<script type="text/javascript" src="//cdn.bootcss.com/canvas-nest.js/1.0.0/canvas-nest.min.js"></script>
	

<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"log":false,"pluginJsPath":"lib/","pluginModelPath":"assets/","pluginRootPath":"live2dw/","tagMode":false});</script></body>
</html>
<!-- 页面点击小红心 -->
<script type="text/javascript" src="/js/src/clicklove.js"></script>
